import axios from 'axios'
import { Message } from 'element-ui'
import api from '../../static/url'
import router from '@/router'


const http = axios.create({
  baseURL: api.path,
  timeout: 5000
})

/**请求拦截器**/
http.interceptors.request.use(
  config => {
    config.headers["token"] = localStorage.token;
    return config
  },
  error => {
    Promise.reject(error)
  }
)

/**响应拦截器**/
http.interceptors.response.use(
  response => {
    const res = response.data;
    if (res.status !== 0) {
      Message({
        message: res.msg,
        type: 'error',
        duration: 5 * 1000
      });
      return Promise.reject('error')
    } else {
      return response.data.data
    }
  },
  error => {
    router.push({path: "/500"});
    //Message.error("服务器超时");
    return Promise.reject(error)
  }
)

export default http
